#include <typescommon.h>
#include <stdarg.h>

#define CONFIG_STRING_VALUE_MAX_SIZE 32
#define getdefinestr(a) #a
struct ObjectConfigInform
{
    const char *name;
    int configType; // 0：int，1：string
    union
    {
        int intval;
        char strval[CONFIG_STRING_VALUE_MAX_SIZE];
    };
};
#define DEFINE_OBJECT_INT(dname, value)                 \
    {                                                   \
        .name = dname, .configType = 0, .intval = value \
    }
#define DEFINE_OBJECT_STRING(dname, value)              \
    {                                                   \
        .name = dname, .configType = 1, .strval = value \
    }

static struct ObjectConfigInform configbuffer[] =
    {
        DEFINE_OBJECT_INT("confInt1", 1),
        DEFINE_OBJECT_INT("confInt2", 2),
        DEFINE_OBJECT_INT("confInt3", 3),
        DEFINE_OBJECT_STRING("confInt4", "abcdefghigklmnopq"),
        DEFINE_OBJECT_STRING("confInt5", ""),
        DEFINE_OBJECT_STRING("confInt6", "def"),
        DEFINE_OBJECT_STRING("netip", "192.168.1.33"),
        DEFINE_OBJECT_STRING("netmask", "255.255.255.0"),
        DEFINE_OBJECT_STRING("netgate", "192.168.1.1"),
        DEFINE_OBJECT_INT("checkcount", 1105),
};

#define CONFIG_SAVE_FILE_PATH "/tmp/objectConfig.txt"

int getConfigFormFile()
{
    FILE *tmpfile;
    tmpfile = fopen(CONFIG_SAVE_FILE_PATH, "r");
    if(!tmpfile)
    {
        logerror("open error %s", CONFIG_SAVE_FILE_PATH);
        return -1;
    }
    char tmpbuffer[512];
    char tmpfmt[256];
    snprintf(tmpfmt, sizeof(tmpfmt), "%%*[^=]=%%%ds\\n", CONFIG_STRING_VALUE_MAX_SIZE - 1);
    logdebug("fmt:%s", tmpfmt);
    while(fgets(tmpbuffer, sizeof(tmpbuffer), tmpfile))
    {
        for(int i = 0; i < ARRAY_LEN(configbuffer); i++)
        {
            if(memcmp(configbuffer[i].name, tmpbuffer, strlen(configbuffer[i].name)) != 0)
            {
                continue;
            }
            // logdebug("%s", configbuffer[i].name);
            if(configbuffer[i].configType)//string
            {
                sscanf(tmpbuffer, tmpfmt, configbuffer[i].strval);
                logdebug("get config,name=%s,value=%s", configbuffer[i].name, configbuffer[i].strval);
            }
            else
            {
                sscanf(tmpbuffer, "%*[^=]=%d\n", &configbuffer[i].intval);
                logdebug("get config,name=%s,value=%d", configbuffer[i].name, configbuffer[i].intval);
            }
        }
    }
    fclose(tmpfile);
    return 0;
}

int setConfigFormFile()
{
    FILE *tmpfile;
    tmpfile = fopen(CONFIG_SAVE_FILE_PATH, "w+");
    if(!tmpfile)
    {
        logerror("open error %s", CONFIG_SAVE_FILE_PATH);
        return -1;
    }
    fprintf(tmpfile, "config value, string size < 20\n");
    for(int i = 0; i < ARRAY_LEN(configbuffer); i++)
    {
        if(configbuffer[i].configType)//string
        {
            fprintf(tmpfile, "%s=%s\n", configbuffer[i].name, configbuffer[i].strval);
            logdebug("set config,name=%s,value=%s", configbuffer[i].name, configbuffer[i].strval);
        }
        else
        {
            fprintf(tmpfile, "%s=%d\n", configbuffer[i].name, configbuffer[i].intval);
            logdebug("set config,name=%s,value=%d", configbuffer[i].name, configbuffer[i].intval);
        }
    }
    fclose(tmpfile);
    return 0;
}

int main(int argc, char **argv)
{
    int tmpflag = getConfigFormFile();
    if(tmpflag < 0)
    {
        setConfigFormFile();
    }
    logdebug("read config file ok!");
    for(int i = 0; i < ARRAY_LEN(configbuffer); i++)
    {
        logdebug("name=%s,value=%d", configbuffer[i].name, configbuffer[i].intval);
    }
    return 1;
}
